<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\wechat\controller;


use service\DataService;
use think\admin\Controller;
use think\facade\Request;
use think\facade\Db;

/**
 * 消息模板管理
 * Class WechatWechatTemplate
 * @package app\wechat\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class WechatWechatTemplate extends Controller
{

    /**
     * 定义当前默认数据表
     * @var string
     */
    public $table = 'wechat_wechat_template';


    /**
     * 显示消息模板列表
     * @auth true  # 表示需要验证权限
     * @menu true  # 在菜单编辑的节点可选项
     */
    public function index()
    {
        $this->title = '消息模板管理';
        $get = $this->request->get();
        $query = $this->_query(Db::connect('zs')->table($this->table))->where('is_deleted',0);
        $query->like('title')->dateBetween('create_at')->order('id desc')->page();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(&$data)
    {

    }

    /**
     * 添加关键字
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $this->title = '添加模板';
        return $this->_form(Db::connect('zs')->table($this->table), 'form');
    }

    /**
     * 编辑关键字
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $this->title = '编辑模板';
        return $this->_form(Db::connect('zs')->table($this->table), 'form');
    }


    /**
     * 删除关键字
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("模板删除成功！", '');
        }
        $this->error("模板删除失败，请稍候再试！");
    }

    /**
     * 关键字禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("模板禁用成功！", '');
        }
        $this->error("模板禁用失败，请稍候再试！");
    }

    /**
     * 关键字禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("模板启用成功！", '');
        }
        $this->error("模板启用失败，请稍候再试！");
    }

    /**
     * 添加数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost() && isset($data['created_at'])) {
            if ($data['created_at'] == '') {
                $data['created_at'] = date("Y/m/d");
            }
        }
    }

    /**
     * 编辑结果处理
     * @param $result
     */
    protected function _form_result($result)
    {
        if ($result !== false) {
            /*list($url, $keys) = ['', $this->request->post('keys')];
            if (!in_array($keys, ['subscribe', 'default'])) {
                $url = url('@admin') . '#' . url('wechat/keys/index') . '?spm=' . $this->request->get('spm');
            }*/
            $url = url('@admin') . '#' . url('wechat/wechat_wechat_template/index') . '?spm=' . $this->request->get('spm');
            $this->success('恭喜, 模板保存成功!', $url);
        }
        $this->error('模板保存失败, 请稍候再试!');
    }


}
